﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml.Linq;

namespace FortnoxAPI
{
    /// <summary>
    /// Get Currencies from Fortnox
    /// </summary>
    public partial class GetCurrencies
    {

        /// <summary>
        /// Gets the values.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="token">The token.</param>
        /// <param name="db">The db.</param>
        /// <returns></returns>
        public static DataTable GetValues(string url, string token, string db)
        {
            XDocument doc = XDocument.Load(url + "token=" + token + "&db=" + db);

            DataTable dt1 = new DataTable();
            dt1.Columns.Add("id", typeof(string));
            dt1.Columns.Add("description", typeof(string));
            dt1.Columns.Add("buyrate", typeof(string));
            dt1.Columns.Add("sellrate", typeof(string));
            dt1.Columns.Add("unit", typeof(string));
            dt1.Columns.Add("modified", typeof(string));


            IEnumerable<XElement> contacts = from contact in doc.Descendants("currency")

                                             //where (string)post.Attribute("id").Value == "1"
                                             select contact;


            foreach (XElement contact in contacts)
            {
                DataRow dr1 = dt1.NewRow();

                if (contact.Element("id") != null)
                {
                    dr1["id"] = contact.Element("id").Value;
                }
                else
                {
                    dr1["id"] = "";
                }

                if (contact.Element("description") != null)
                {
                    dr1["description"] = contact.Element("description").Value;
                }
                else
                {
                    dr1["description"] = "";
                }

                if (contact.Element("buyrate") != null)
                {
                    dr1["buyrate"] = contact.Element("buyrate").Value;
                }
                else
                {
                    dr1["buyrate"] = "";
                }

                if (contact.Element("sellrate") != null)
                {
                    dr1["sellrate"] = contact.Element("sellrate").Value;
                }
                else
                {
                    dr1["sellrate"] = "";
                }

                if (contact.Element("unit") != null)
                {
                    dr1["unit"] = contact.Element("unit").Value;
                }
                else
                {
                    dr1["unit"] = "";
                }

                if (contact.Element("modified") != null)
                {
                    dr1["modified"] = contact.Element("modified").Value;
                }
                else
                {
                    dr1["modified"] = "";
                }

               
                dt1.Rows.Add(dr1);
            }
            return dt1;
        }

    }
}
